package com.bytecodezz.app.sketch.main.ui.welcome

import android.graphics.drawable.Animatable
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import com.bytecodezz.app.sketch.common.base.BaseUIFragment
import com.bytecodezz.app.sketch.main.model.LauncherUIViewModel
import com.bytecodezz.app.sketch.main.model.WelcomeFragmentViewModel
import com.bytecodezz.app.base.framework.base.model.BaseViewModel
import com.bytecodezz.app.base.framework.tools.StatusBarTools
import com.bytecodezz.app.sketch.main.databinding.MainFragmentWelcomeBinding

/**
 * @author: bytecodezz
 * @version: 1.0
 * @time: 2021/7/6 18:29
 * @package: com.bytecodezz.app.ui.launcher
 *
 * @description:
 *
 */
class WelcomeFragment : BaseUIFragment<MainFragmentWelcomeBinding, WelcomeFragmentViewModel>() {
    private var isFirst: Boolean = true
    override fun onCreateViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = MainFragmentWelcomeBinding.inflate(inflater, container, false)

    override fun onCreateViewModelClass() = WelcomeFragmentViewModel::class.java

    override fun onActivityViewModelProvider(): BaseViewModel? {
        return activity?.let { ViewModelProvider(it).get(LauncherUIViewModel::class.java) }
    }

    override fun initOtherObserver() {
        viewModel?.let {
            it.screenChangeEvent.observe(this) { isFullScreen ->
                if (isFullScreen) {
                    StatusBarTools.enterFullScreen(requireActivity())
                } else {
                    StatusBarTools.quitFullScreen(requireActivity())
                }
            }
            it.finishPagerEvent.observe(this) { isFinish ->
                if (isFinish) {
                    viewModel?.screenStatusChange(false)
                    getActivityViewModel<LauncherUIViewModel>()?.let { vm ->
                        vm.hintWelcome()
                    }
                }
            }

            it.countDownContentEvent.observe(this) { skipTime ->
            }
        }
    }

    override fun onActionStart(contentView: View) {
        super.onActionStart(contentView)

        viewModel?.initLoadData()
//        isFirst = BaseApplication.application.getSynchroBoolean("is_first", defaultValue = true)
//        if (isFirst) {
//            //第一次进入，记录为安装时间
//            GlobalScope.launch {
//                BaseApplication.application.put("is_first", false)
//                BaseApplication.application.put("install_time", System.currentTimeMillis())
//            }
//        } else {
//
//        }
        //开始计时，不管广告的获取过程了，直接从4秒开始计时，广告获取到了的话显示倒计时按钮即可，如果没获取成功，计时结束自动跳转了
        binding.welcomeTextImv.drawable?.let {
            if (it is Animatable) {
                it.start()
            }

        }
        viewModel?.startTimer()
    }

    override fun onStart() {
        super.onStart()
        if (!isHidden) {
            viewModel?.screenStatusChange(true)
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        viewModel?.screenStatusChange(!hidden)
    }
}